using System;
using System.Drawing;
using System.Windows.Forms;
using Spread.Common.DataStore;
using FarPoint.Win.Spread;

namespace Spread.Filtering.Features
{
    public partial class FilterBar : Spread.Common.Features.Sample.BaseSampleControl
    {
        private AccessDataSource _dataSource;

        #region ctor
        public FilterBar()
        {
            InitializeComponent();
            Name = "Filter Bar";
            _dataSource = new DataService().AccessDataSource;

            InitSpreadSheet(fpSpread.ActiveSheet);
            InitControls(fpSpread.ActiveSheet);
        }
        #endregion

        private void InitSpreadSheet(SheetView sheet)
        {
            sheet.DataSource = _dataSource.GetDataFrom("fpWine.mdb", "select OrderID, CustomerID," +
                "SalesPerson, OrderDate, ShippedDate, Freight from Orders");
            
            sheet.Columns[2].Width = 120;
            sheet.Columns[3, 4].Width = 80;
            sheet.Columns[5].Width = 70;

            sheet.SetMultipleRowHeights(0, sheet.RowCount, 20);

            Random random = new Random();
            var colors = Enum.GetValues(typeof(KnownColor));
            for (int row = random.Next(0, 4); row < sheet.RowCount; row += random.Next(1, 5))
            {
                sheet.Cells[row, 1].BackColor = Color.FromKnownColor((KnownColor)(colors.GetValue(
                    random.Next(27, colors.GetUpperBound(0) + 1))));
            }

            sheet.AutoFilterMode = AutoFilterMode.FilterBar;
        }

        private void InitControls(SheetView sheet)
        {
            comboGridLine.Items.AddRange(new object[] { "HeaderHorizontalGridLine", "HeaderVerticalGridLine",
                "HorizontalGridLine", "VerticalGridLine" });
            comboGridLine.SelectedIndexChanged += ComboGridLine_SelectedIndexChanged;
            comboGridLine.SelectedIndex = 0;

            comboGridType.DataSource = Enum.GetValues(typeof(GridLineType));

            btnColor.Click += ShowColorPicker;
            btnHighlightColor.Click += ShowColorPicker;
            btnShadowColor.Click += ShowColorPicker;
            btnUpdate.Click += BtnUpdate_Click;
        }

        private GridLine GetFilterBarGridLine(string name)
        {
            switch (name)
            {
                case "HeaderHorizontalGridLine":
                    return fpSpread.ActiveSheet.FilterBarHeaderHorizontalGridLine;
                case "HeaderVerticalGridLine":
                    return fpSpread.ActiveSheet.FilterBarHeaderVerticalGridLine;
                case "HorizontalGridLine":
                    return fpSpread.ActiveSheet.FilterBarHorizontalGridLine;
                case "VerticalGridLine":
                    return fpSpread.ActiveSheet.FilterBarVerticalGridLine;
                default:
                    return null;
            }
        }

        #region Event handlers
        private void ShowColorPicker(object sender, EventArgs e)
        {
            Button button = sender as Button;
            ColorDialog dialog = new ColorDialog();
            dialog.Color = button.BackColor;

            if (dialog.ShowDialog() == DialogResult.OK)
                button.BackColor = dialog.Color;
        }

        private void BtnUpdate_Click(object sender, EventArgs e)
        {
            GridLine gridLine = new GridLine((GridLineType)comboGridType.SelectedItem, btnColor.BackColor, btnHighlightColor.BackColor,
                btnShadowColor.BackColor, numericUpDown.Value == -1 ? 1 : (int)numericUpDown.Value);
            SheetView sheet = fpSpread.ActiveSheet;

            switch (comboGridLine.SelectedItem.ToString())
            {
                case "HeaderHorizontalGridLine":
                    sheet.FilterBarHeaderHorizontalGridLine = gridLine;
                    break;
                case "HeaderVerticalGridLine":
                    sheet.FilterBarHeaderVerticalGridLine = gridLine;
                    break;
                case "HorizontalGridLine":
                    sheet.FilterBarHorizontalGridLine = gridLine;
                    break;
                case "VerticalGridLine":
                    sheet.FilterBarVerticalGridLine = gridLine;
                    break;
            }
        }

        private void ComboGridLine_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridLine gridLine = GetFilterBarGridLine(comboGridLine.SelectedItem.ToString());

            comboGridType.SelectedItem = gridLine.Type;
            btnColor.BackColor = gridLine.Color;
            btnHighlightColor.BackColor = gridLine.HighlightColor;
            btnShadowColor.BackColor = gridLine.ShadowColor;
            numericUpDown.Value = gridLine.Width;
        }
        #endregion
    }
}


//
//FilterBar.Designer.cs
//


namespace Spread.Filtering.Features
{
    partial class FilterBar
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fpSpread = new FarPoint.Win.Spread.FpSpread();
            this.fpSpread_Sheet1 = new FarPoint.Win.Spread.SheetView();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.btnUpdate = new System.Windows.Forms.Button();
            this.numericUpDown = new System.Windows.Forms.NumericUpDown();
            this.label6 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.btnShadowColor = new System.Windows.Forms.Button();
            this.btnHighlightColor = new System.Windows.Forms.Button();
            this.label4 = new System.Windows.Forms.Label();
            this.btnColor = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.comboGridType = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.comboGridLine = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).BeginInit();
            this.groupBox1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this.groupBox1);
            // 
            // fpSpread
            // 
            this.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread.Location = new System.Drawing.Point(0, 0);
            this.fpSpread.Name = "fpSpread";
            this.fpSpread.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.fpSpread_Sheet1});
            this.fpSpread.Size = new System.Drawing.Size(477, 487);
            this.fpSpread.Skin = FarPoint.Win.Spread.DefaultSpreadSkins.Office2016White;
            this.fpSpread.StatusBarVisible = true;
            this.fpSpread.TabIndex = 0;
            // 
            // fpSpread_Sheet1
            // 
            this.fpSpread_Sheet1.Reset();
            this.fpSpread_Sheet1.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.btnUpdate);
            this.groupBox1.Controls.Add(this.numericUpDown);
            this.groupBox1.Controls.Add(this.label6);
            this.groupBox1.Controls.Add(this.label5);
            this.groupBox1.Controls.Add(this.btnShadowColor);
            this.groupBox1.Controls.Add(this.btnHighlightColor);
            this.groupBox1.Controls.Add(this.label4);
            this.groupBox1.Controls.Add(this.btnColor);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.comboGridType);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.comboGridLine);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(157, 304);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "FilterBar Settings";
            // 
            // btnUpdate
            // 
            this.btnUpdate.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.btnUpdate.Location = new System.Drawing.Point(45, 250);
            this.btnUpdate.Name = "btnUpdate";
            this.btnUpdate.Size = new System.Drawing.Size(75, 23);
            this.btnUpdate.TabIndex = 11;
            this.btnUpdate.Text = "Update";
            this.btnUpdate.UseVisualStyleBackColor = true;
            // 
            // numericUpDown
            // 
            this.numericUpDown.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.numericUpDown.Location = new System.Drawing.Point(63, 213);
            this.numericUpDown.Minimum = new decimal(new int[] {
            1,
            0,
            0,
            -2147483648});
            this.numericUpDown.Name = "numericUpDown";
            this.numericUpDown.Size = new System.Drawing.Size(88, 20);
            this.numericUpDown.TabIndex = 10;
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(9, 215);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(38, 13);
            this.label6.TabIndex = 9;
            this.label6.Text = "Width:";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(8, 175);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(49, 26);
            this.label5.TabIndex = 8;
            this.label5.Text = "Shadow \r\nColor:";
            // 
            // btnShadowColor
            // 
            this.btnShadowColor.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.btnShadowColor.Location = new System.Drawing.Point(63, 176);
            this.btnShadowColor.Name = "btnShadowColor";
            this.btnShadowColor.Size = new System.Drawing.Size(88, 23);
            this.btnShadowColor.TabIndex = 7;
            this.btnShadowColor.UseVisualStyleBackColor = true;
            // 
            // btnHighlightColor
            // 
            this.btnHighlightColor.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.btnHighlightColor.Location = new System.Drawing.Point(61, 129);
            this.btnHighlightColor.Name = "btnHighlightColor";
            this.btnHighlightColor.Size = new System.Drawing.Size(90, 23);
            this.btnHighlightColor.TabIndex = 7;
            this.btnHighlightColor.UseVisualStyleBackColor = true;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(9, 127);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(51, 26);
            this.label4.TabIndex = 6;
            this.label4.Text = "Highlight \r\nColor:";
            // 
            // btnColor
            // 
            this.btnColor.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.btnColor.Location = new System.Drawing.Point(61, 89);
            this.btnColor.Name = "btnColor";
            this.btnColor.Size = new System.Drawing.Size(90, 23);
            this.btnColor.TabIndex = 5;
            this.btnColor.UseVisualStyleBackColor = true;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(9, 94);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(34, 13);
            this.label3.TabIndex = 4;
            this.label3.Text = "Color:";
            // 
            // comboGridType
            // 
            this.comboGridType.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.comboGridType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboGridType.FormattingEnabled = true;
            this.comboGridType.Location = new System.Drawing.Point(61, 57);
            this.comboGridType.Name = "comboGridType";
            this.comboGridType.Size = new System.Drawing.Size(90, 21);
            this.comboGridType.TabIndex = 3;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(6, 60);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(34, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "Type:";
            // 
            // comboGridLine
            // 
            this.comboGridLine.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.comboGridLine.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboGridLine.FormattingEnabled = true;
            this.comboGridLine.Location = new System.Drawing.Point(61, 22);
            this.comboGridLine.Name = "comboGridLine";
            this.comboGridLine.Size = new System.Drawing.Size(90, 21);
            this.comboGridLine.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(6, 25);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(49, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "GridLine:";
            // 
            // SpreadFilterBar
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "SpreadFilterBar";
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread;
        private FarPoint.Win.Spread.SheetView fpSpread_Sheet1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.ComboBox comboGridType;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.ComboBox comboGridLine;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button btnColor;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.NumericUpDown numericUpDown;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Button btnShadowColor;
        private System.Windows.Forms.Button btnHighlightColor;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Button btnUpdate;
    }
}
